<template>
  <div  >
    <!--      标题-->
    <div class="title">出货统计</div>
    <div style="display: flex;margin-top: 5px;margin-bottom: 5px;">
      <JButton  @click="setDay(1)" :type="current===1?'info':'default'">今天</JButton>
      <JButton  @click="setDay(2)" :type="current===2?'info':'default'">昨天</JButton>
      <JButton  @click="setDay(3)" :type="current===3?'info':'default'">前天</JButton>
      <div  style="margin-left: 10px; width: 240px;margin-right: 30px;display: flex;">
        <JPicker style="width: 100%" v-model="beginDate" size="small" type="daterange" /> -
        <JPicker style="width: 100%" v-model="endDate" size="small" type="daterange" />
      </div>
      <div style="margin-right: 10px;width: 180px;">
        <JInput v-model="billNo" placeholder="票号" />
      </div>
      <JDeliver size="small" v-model="deliverNames" @change="search"></JDeliver>
      <JButton  @click="search" style="margin-left: 10px;" icon="search" type="info">搜索</JButton >
      <JButton  @click="download" style="margin-left: 10px;" icon="download">下载</JButton >
      <JButton  @click="clear" style="margin-left: 10px;" icon="refresh">刷新</JButton >
    </div>
    <div>
      <j-grid bind-data v-model="list.cargo" size="mini"  :columns="cols1" :offset-bottom="400" :offset-width="105" />
    </div>
    <div style="margin-top: 2px;">
      <j-grid ref="gref" size="mini" show-footer   :columns="cols2" :offset-bottom="1" bind-data v-model="list.goods"  :offset-width="105" />
    </div>
  </div>
</template>
<script setup>
// 出统计
import JDeliver from "@/views/ps/JDeliver.vue";
import tool from "@/utils/tool";
import {computed, onBeforeMount, ref} from "vue";
import JPicker from "@/lui/JPicker.vue";
//
const cols1 = ref( [
  {type: 'index', width: 70},
  {field: 'psBillNo', title: '配送号', width: 130,headerAlign:'center',},
  {field: 'psBillDate', title: '配送时间', width: 100,headerAlign:'center',},
  // {field: 'billBillNo', title: '销售票号', width: 160,align: 'left',headerAlign:'center'},
  // {field: 'buName', title: '终端名称',align: 'left',headerAlign:'center'},
  {field: 'deliverNames', title: '送货人', width: 150,align: 'left',headerAlign:'center'},
  {field: 'stat', title: '状态',align: 'center',width: 80,headerAlign:'center' },
])
const cols2 = ref([
  {type: 'index', width: 70},
  {field: 'invName', title: '商品名称',align:'left',headerAlign:'center',},
  {field: 'spec', title: '规格', width: 80,align: 'left',headerAlign:'center'},
  {field: 'qty', title: '数量',align: 'right' ,width: 150,sum: true,headerAlign:'center',},
  {field: 'unit', title: '单位', width: 80,align: 'center',headerAlign:'center',},
  {field: 'assQty', title: '辅助数量',align: 'right' ,width: 150,headerAlign:'center'},
])
const list = ref({
  cargo: [],
  goods: []
})
const current = ref(1)
const beginDate = ref(tool.getDay())
const endDate = ref(tool.getDay())
const billNo = ref('')
const deliverNames = ref('')
onBeforeMount(() => {
  search()
})
const goods = computed(() => {
  let ret = []
  let gp = tool.xe.groupBy(list.value.goods, 'invName')
  for(let i in gp) {
    let good = {
      invName: gp[i][0].invName,
      specAss: gp[i][0].specAss,
      unit: gp[i][0].unit,
      qty: tool.xe.sum(gp[i], 'qty'),
    }
    ret.push(good)
  }
  return tool.xe.orderBy(ret, [['qty', 'desc']])
})
const clear = () => {
  list.value = {
    cargo: [],
    goods: []
  }
  billNo.value = ''
  search()
}
const setDay = (index) => {
  current.value = index
  if(index === 1) {
    beginDate.value = tool.getDay()
    endDate.value = tool.getDay()
  }
  if(index === 2) {
    beginDate.value = tool.getDay('', -1)
    endDate.value = tool.getDay('', -1)
  }
  if(index === 3) {
    beginDate.value = tool.getDay('', -2)
    endDate.value = tool.getDay('', -2)
  }
  search(0)
}
const download = () => {
  search(1)
}
const search = (excel = 0) => {
  list.value.cargo = []
  list.value.goods = []
  tool.post('justdeliver/gpCargos', {
    beginDate: beginDate.value,
    endDate: endDate.value,
    deliverNames: deliverNames.value,
    billNo: billNo.value,
    excel
  }, true).then(ret => {
    if (ret.stat && ret.data) {
      if(excel === 1) {
        if(ret.data) {
          if(typeof qt !== 'undefined') {
            tool.sendToPy({name: 'download', url: ret.data })
          }
          else {
            window.open(ret.data)
          }
        }
      }
      else {
        list.value.cargo = ret.data.cargo
        list.value.goods = ret.data.goods
      }
    }
  })
}

</script>

<style scoped lang="scss">
.title {
  text-align: center;
  font-weight: bold;
  font-size: 22px;
  background-color: #01aaee;
  color: white;
  height: 40px;
  line-height: 40px;
}
</style>